/*
 * Copyright (c) 2006, 2007 Borland Software Corporation
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Dmitry Stadnik (Borland) - initial API and implementation
 */
package dataflowScheme.diagram.edit.policies;

import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.ui.dialogs.WorkspaceResourceDialog;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gmf.runtime.common.core.command.CommandResult;
import org.eclipse.gmf.runtime.common.core.command.OneTimeCommand;
import org.eclipse.gmf.runtime.diagram.ui.commands.ICommandProxy;
import org.eclipse.gmf.runtime.diagram.ui.editpolicies.OpenEditPolicy;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;

import dataflowScheme.Storage;
import dataflowScheme.Task;

/**
 * @generated
 */
public class OpenPECoreFilePolicy extends OpenEditPolicy {

	public OpenPECoreFilePolicy() {
		// TODO Auto-generated constructor stub
		super();
	}
	
	/**
	 * @generated
	 */
	protected Command getOpenCommand(Request request) {
		EditPart targetEditPart = getTargetEditPart(request);
		if (false == targetEditPart.getModel() instanceof View) {
			return null;
		}
		View view = (View) targetEditPart.getModel();
//		Style link = view.getStyle(NotationPackage.eINSTANCE.getHintedDiagramLinkStyle());
//		if (false == link instanceof HintedDiagramLinkStyle) {
//			return null;
//		}
		String fileToOpen = "/test/src/MyTest.java";
		
//		EList list = view.getChildren();
//		Object o = list.get(0);
//		if(o instanceof Node){
//			Node node = (Node)o;
			EObject element = view.getElement();
			if (element instanceof Task){
				Task task = (Task)element;
				Object o = task.getSource();
				if(o == null){
					return null;
				}
				fileToOpen = (String)o;
			} else if (element instanceof Storage){
				Storage storage = (Storage)element;
				Object o = storage.getSource();
				if(o == null){
					return null;
				}
				fileToOpen = (String)o;
			}
//		}
		return new ICommandProxy(new OpenFileCommand(fileToOpen));
	}

	protected class OpenFileCommand extends OneTimeCommand {

		private String fileToOpen = "";
		
		public OpenFileCommand(String file) {
			// TODO Auto-generated constructor stub
			super("Open file");
			this.fileToOpen = file;
		}

		/**
		 * @generated
		 */
		protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException {
			try {
//				Diagram diagram = getDiagramToOpen();
//				if (diagram == null) {
//					diagram = intializeNewDiagram();
//				}
//				URI uri = EcoreUtil.getURI(diagram);
//				String editorName = uri.lastSegment() + "#" + diagram.eResource().getContents().indexOf(diagram); //$NON-NLS-1$

				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				
//				Shell shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell();
//				IFile file2 = WorkspaceResourceDialog.openNewFile(shell, "tajtl", "misidz", null, null);
				
				IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(fileToOpen));
				IEditorInput inputEditor = new FileEditorInput(file);
				String id = PlatformUI.getWorkbench().getEditorRegistry().getDefaultEditor(fileToOpen).getId();
				page.openEditor(inputEditor, id);
				
//				IEditorInput editorInput = new URIEditorInput(uri, editorName);
//				page.openEditor(editorInput, getEditorID());

				return CommandResult.newOKCommandResult();
			} catch (Exception ex) {
				throw new ExecutionException("Can't open diagram", ex);
			}
		}

//		/**
//		 * @generated
//		 */
//		protected Diagram getDiagramToOpen() {
//			return diagramFacet.getDiagramLink();
//		}
//
//		/**
//		 * @generated
//		 */
//		protected Diagram intializeNewDiagram() throws ExecutionException {
//			Diagram d = ViewService.createDiagram(getDiagramDomainElement(), getDiagramKind(), getPreferencesHint());
//			if (d == null) {
//				throw new ExecutionException("Can't create diagram of '" + getDiagramKind() + "' kind");
//			}
//			diagramFacet.setDiagramLink(d);
//			assert diagramFacet.eResource() != null;
//			diagramFacet.eResource().getContents().add(d);
//			EObject container = diagramFacet.eContainer();
//			while (container instanceof View) {
//				((View) container).persist();
//				container = container.eContainer();
//			}
//			try {
//				new WorkspaceModifyOperation() {
//
//					protected void execute(IProgressMonitor monitor) throws CoreException, InvocationTargetException, InterruptedException {
//						try {
//							for (Iterator it = diagramFacet.eResource().getResourceSet().getResources().iterator(); it.hasNext();) {
//								Resource nextResource = (Resource) it.next();
//								if (nextResource.isLoaded() && !getEditingDomain().isReadOnly(nextResource)) {
//									nextResource.save(TaiPanDiagramEditorUtil.getSaveOptions());
//								}
//							}
//						} catch (IOException ex) {
//							throw new InvocationTargetException(ex, "Save operation failed");
//						}
//					}
//				}.run(null);
//			} catch (InvocationTargetException e) {
//				throw new ExecutionException("Can't create diagram of '" + getDiagramKind() + "' kind", e);
//			} catch (InterruptedException e) {
//				throw new ExecutionException("Can't create diagram of '" + getDiagramKind() + "' kind", e);
//			}
//			return d;
//		}
//
//		/**
//		 * @generated
//		 */
//		protected EObject getDiagramDomainElement() {
//			// use same element as associated with EP
//			return ((View) diagramFacet.eContainer()).getElement();
//		}
//
//		/**
//		 * @generated
//		 */
//		protected PreferencesHint getPreferencesHint() {
//			// XXX prefhint from target diagram's editor?
//			return TaiPanDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT;
//		}
//
//		/**
//		 * @generated
//		 */
//		protected String getDiagramKind() {
//			return "Port";
//		}
//
//		/**
//		 * @generated
//		 */
//		protected String getEditorID() {
//			return "org.eclipse.gmf.examples.taipan.port.diagram.part.PortDiagramEditorID";
//		}
	}

}
